﻿import QtQuick 2.12
import QtQuick.Controls 2.12
import "../components"

// 蓝色按钮的执行弹窗
CustomPopupBox {

    id: root

    title: "提示"
    height: 270*dp; width: 400*dp

    property int partId: -1         // 正在执行的模块

    property int infoState: 0       // -1未显示 0等待响应 1开始启动 2启动完成 3响应超时 4执行超时


    function startWait01(){
        infoState = 0
        timer_timeout0.start()
        timer_timeout1.stop()
    }function startWait02(){
        infoState = 1
        timer_timeout1.start()
        timer_timeout0.stop()
    }
    function finishWait(){
        infoState = 2
        timer_timeout0.stop()
        timer_timeout1.stop()
    }
    function stopTimer(){
        timer_timeout0.stop()
        timer_timeout1.stop()
    }

    function setTimer1Interval(interval){
        timer_timeout1.interval = interval
    }


    Timer{      // 等待响应计时0
        id: timer_timeout0
        interval: config.return01Timeout
        onTriggered: {
            infoState = 3
            gExpIsExecOperate = 0

        }
    }

    Timer{      // 等待执行计时1
        id: timer_timeout1
        interval: config.return02Timeout_short
        onTriggered: {
            infoState = 4
            gExpIsExecOperate = 0
        }
    }

    contentItem: Item{

        CustomAnimatedImage{      // 等待中
            id: gif_resetting_big
            source: "qrc:/picture/loading.gif"
            anchors.verticalCenterOffset: -60*dp
            anchors.centerIn: parent
            visible: infoState === 0 || infoState === 1
            playing: visible
            speed: 2
            height: 80*dp; width: 80*dp
        }Image {            // 超时
            source: "qrc:/picture/msgError.png"
            visible: infoState===3 || infoState === 4
            anchors.centerIn: parent
            anchors.verticalCenterOffset: -60*dp
            height: 56*dp; width: 56*dp
        }                   // 完成
        Image {
            id: name
            anchors.centerIn: parent
            anchors.verticalCenterOffset: -60*dp
            height: 56*dp
            width: 56*dp
            visible: infoState===2
            source: "qrc:/picture/msgSuccess.png"
        }

        Text{
            y: 100*dp
            text: {
                if(infoState === 0)     return "正在等待设备响应"
                if(infoState === 1)     return "正在等待设备执行指令"
                if(infoState === 2)     return "执行完成"
                if(infoState === 3)     return "设备响应超时"
                if(infoState === 4)     return "设备执行指令超时"
                return ""
            }
            font.pixelSize: config.fontsize
            anchors.horizontalCenter: parent.horizontalCenter
        }
        CustomButton{
            y: 150*dp
            visible: infoState < 2
            defaultMode2: true
            title: "关闭"
            anchors.horizontalCenter: parent.horizontalCenter
            onClicked: {
                box_warning.show()
            }
        }
        CustomButton{
            y: 150*dp
            visible: infoState >= 2
            defaultMode2: true
            title: "关闭"
            anchors.horizontalCenter: parent.horizontalCenter
            onClicked: {
                close()
                gExpIsExecOperate = 0
                infoState = -1
            }
        }
    }


    onInfoStateChanged: {           // 状态切换时，强制关闭弹窗关闭，通知用户执行完成
        if(infoState >= 2){
            box_warning.close()
        }
    }

    CustomPopupBoxWarning{
        id: box_warning
        box_text: "当前指令未执行完成，关闭弹窗后进行其它操作可能会导致设备异常。确认关闭吗？"
        onAccepted: {
            gExpIsExecOperate = 0
            timer_timeout0.stop()
            timer_timeout1.stop()
            root.close()
        }
    }
}
